package com.test.utils;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.util.Enumeration;

/**
 * pfx公私钥解密
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/20
 */
@Slf4j
public class ReadPFX {


    public ReadPFX (){
    }
        //转换成十六进制字符串
        public static String Byte2String(byte[] b) {
        String hs="";
        String stmp="";

        for (int n=0;n<b.length;n++) {
            stmp=(Integer.toHexString(b[n] & 0XFF));
            if (stmp.length()==1) hs=hs+"0"+stmp;
            else hs=hs+stmp;
        }
        return hs.toUpperCase();
    }

        public static byte[] StringToByte(int number) {
        int temp = number;
        byte[] b=new byte[4];
        for (int i=b.length-1;i>-1;i--){
            b[i] = new Integer(temp&0xff).byteValue();
            temp = temp >> 8;
        }
        return b;
    }
        public  PrivateKey GetPvkformPfx(String strPfx, String strPassword){
        try {
            KeyStore ks = KeyStore.getInstance("PKCS12");
            FileInputStream fis = new FileInputStream(strPfx);
             char[] nPassword = null;
            if ((strPassword == null) || strPassword.trim().equals("")){
                nPassword = null;
            }
            else
            {
                nPassword = strPassword.toCharArray();
            }
            ks.load(fis, nPassword);
            fis.close();

            Enumeration enumas = ks.aliases();
            String keyAlias = null;
            if (enumas.hasMoreElements())
            {
                keyAlias = (String)enumas.nextElement();

            }
            PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword);
            Certificate cert = ks.getCertificate(keyAlias);
            PublicKey pubkey = cert.getPublicKey();

            return prikey;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

        public static void main(String[] args) throws Exception {
        String strPfx = "D:\\sign.pfx";
        String strPassword ="123";
        KeyStore ks = KeyStore.getInstance("PKCS12");
        FileInputStream fis = new FileInputStream(strPfx);
        char[] nPassword = null;
        if ((strPassword == null) || strPassword.trim().equals("")){
            nPassword = null;
        }
        else
        {
            nPassword = strPassword.toCharArray();
        }
        ks.load(fis, nPassword);
        fis.close();
        Enumeration enumas = ks.aliases();
        String keyAlias = null;
        if (enumas.hasMoreElements())
        {
            keyAlias = (String)enumas.nextElement();
        }
        PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword);
        Certificate cert = ks.getCertificate(keyAlias);
        PublicKey pubkey = cert.getPublicKey();
        log.info("public key = " + JSON.toJSONString(pubkey.getEncoded()));
        log.info("private key = " + JSON.toJSONString(prikey.getEncoded()));
    }
    }



